//uses jquery
//uses jquery.placeholder
//uses google maps
//uses kavadenn.PlacesService

var kavadenn = kavadenn || {};

kavadenn.PlaceApplication = function(community,settings){
  var defaultSettings = {
    "lat":48.43,
    "lon":-4.73
  };
  var _settings = {};
  $.extend(_settings, defaultSettings, settings);
  
  this.getSetting = function(key) { return _settings[key]; }
  
  this.community = community;
  this.map = null;
  this.markers = [];
  
  this.initializeUI();
  this.initializeMap();  
  
  this.placesService = new kavadenn.PlacesService(this.community,
    //got data
    this._bindCallback(function(d){
      $("#dataLoadingIndicator").hide();
      for(var i=0; i<d.places.length; i++)
        this.addPlace2Map(d.places[i],d.category);
    }),
    //before getting data
    function(){
      $("#dataLoadingIndicator").show();  
    }
  );
}

kavadenn.PlaceApplication.prototype.initializeUI = function(){
  $('input').placeholder();
  
  //selecting/deselecting categories
  $(".categorySelector").click(this._bindCallback(function(e){
    var checked = $(e.target).attr("checked");
    
    if(checked){
      this.placesService.get({"category":$(e.target).val()});  
    }else{
      this.removePlaces($(e.target).val());
    }
  }));
 
}

kavadenn.PlaceApplication.prototype.initializeMap = function(){
  var myOptions = {
    zoom: 13,
    center: new google.maps.LatLng(this.getSetting("lat"), this.getSetting("lon")),
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    mapTypeControl: true,
    mapTypeControlOptions: {
      style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
      position: google.maps.ControlPosition.BOTTOM},
    navigationControl: true,
    navigationControlOptions: {
      style: google.maps.NavigationControlStyle.ZOOM_PAN,
      position: google.maps.ControlPosition.TOP_RIGHT}
  };
  
  this.map = new google.maps.Map($("#map-canvas")[0], myOptions);  
}

kavadenn.PlaceApplication.prototype.removePlaces = function(category){
  var markers = this.markers;
  for(var i=0; i<markers.length; i++){
    if($.grep(markers[i][0].categories,
      function(e,i){return e == category;}).length != 0)
      markers[i][1].setMap(null);
  }
}

kavadenn.PlaceApplication.prototype.addPlace2Map = function(place,category){
  var markers = this.markers;
  
  var marker = new google.maps.Marker({
    position: new google.maps.LatLng(place.lat, place.lon),
    map: this.map, title: place.name,
    icon: "/static/img/wires/places/" + category + ".png" 
  });

  markers.push([place,marker]);
       
  google.maps.event.addListener(marker, 'click', 
    function(e) {
      var m = marker; 
      var r = $.grep(markers,function(e,i){ return e[1] == m;});
  
      if(r.length != 0) alert(r[0][0].name);  
  });
}

kavadenn.PlaceApplication.prototype._bindCallback = function(callback) {
    //utility method to bind callback methods to the object instance
    var objSelf = this;
    return (function() { return (callback.apply(objSelf, arguments)); });
}
